package ssn.common.menu.service.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import ssn.common.menu.service.MenuService;
import ssn.common.menu.vo.MenuConstants;
import egovframework.rte.fdl.string.EgovStringUtil;

@SuppressWarnings({"rawtypes","unchecked"})
public class MenuServiceImpl implements MenuService{
	
	@Autowired
    protected ServletContext servletContext;
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	private List<String[]> menus;
	
	/**
	 * 생성자
	 * @throws Exception
	 */
	public MenuServiceImpl() throws Exception {
		this.setMenu();
	}
	
	/**
	 * 메뉴정보
	 */
	public Map getTopMenu(String auth, HttpServletRequest request) throws Exception{
		Map result = new HashMap();
		List<String[]> topMenu = new ArrayList<String[]>();
		String nowMenuGroup = "";
		for(String[] menuArray : menus){
			if(EgovStringUtil.equals(menuArray[0], auth)){
				topMenu.add(menuArray);
				if(menuArray[5].equals(request.getServletPath())){
					nowMenuGroup = menuArray[4];
				}
			}
		}
		
		result.put("topMenu", getMenuListSort(topMenu));
		result.put("nowMenuGroup", nowMenuGroup);
		result.put("navi", getMenuListSort(getNavi(nowMenuGroup, topMenu, new ArrayList<String[]>())));
		return result;
	}
	
	/**
	 * 네비 정보
	 * @param nowMenuGroup
	 * @param topMenu
	 * @param navi
	 * @return
	 * @throws Exception
	 */
	public List<String[]> getNavi(String nowMenuGroup, List<String[]> topMenu, List<String[]> navi) throws Exception{
		for(String[] menu : topMenu){
			if(menu[4].equals(nowMenuGroup)){
				navi.add(menu);
			}
		}
		if(nowMenuGroup.length() > 3){
			nowMenuGroup = nowMenuGroup.substring(0, nowMenuGroup.length()-2);
		}else{
			return navi;
		}
		return getNavi(nowMenuGroup, topMenu, navi);
	}
	
	/**
	 * 메뉴 정렬
	 * @param list
	 * @return
	 */
	public List<String[]> getMenuListSort(List<String[]> list){
		Collections.sort(list, new Comparator<String[]>(){
			@Override
			public int compare(String[] o1, String[] o2) {
				// TODO Auto-generated method stub
				String order1 = o1[1];	// menu order
				String order2 = o2[1];	// menu order
				int compare = Integer.parseInt(order1) - Integer.parseInt(order2);
				return compare==0 ? order1.compareTo(order2) : compare;
			}
		});
		return list;
	}
	
	/**
	 * 메뉴정보를 리스트로 만든다.
	 * @throws Exception
	 */
	public void setMenu() throws Exception{
		menus = new ArrayList<String[]>();
		Class menuClass = MenuConstants.class;
		for(Field menuField : menuClass.getFields()) {  
            String[] menuArray = (String[])menuField.get(menus);
            menus.add(menuArray);
        } 
	}

}
